package mt.ctrl;

import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import base.ctrl.BaseController;
import base.jfinal.JsonUtil;
import base.jfinal.anatation.RouteBind;
import base.model.SessionModel;
import java.util.ArrayList;
import java.util.HashMap;

@RouteBind(path = "eval")
public class EvalCtrl extends BaseController {

	public void findDemo() {
		String selectSql = "select mtte.id,mtu.headimgurl,mtt.themename,mtu.username,mtte.evalstar,mtte.content,mtte.createdate,mtte.`like`,mtte.photo,mtte.reply ";
		List<Record> list = Db.paginate(getParaToInt("page", 1), getParaToInt("pagesize", 10), selectSql,
				getSql("eval.find", getParas())).getList();
		this.renderJson((new JsonUtil(list, "photo")).toString());
	}

	// 给app调用分页查询
	public void find() {
		String selectSql = "select mtte.id,mtu.headimgurl,mtt.themename,mtu.username,mtte.evalstar,mtte.content,mtte.createdate,mtte.`like`,mtte.photo,mtte.reply ";
		List<Record> list = Db.paginate(getParaToInt("page", 1), getParaToInt("pagesize", 10), selectSql,
				getSql("eval.find", getParas())).getList();
		this.renderJson((new JsonUtil(list, "photo")).toString());
	}

	// 给app调用分页查询
	public void addLike() {

		// insert into mt_theme_eval_like
		String selectSql = "select mtu.headimgurl,mtt.themename,mtu.username,mtte.evalstar,mtte.content,mtte.createdate,mtte.`like`,mtte.photo,mtte.reply ";
		List<Record> list = Db.paginate(getParaToInt("page", 1), getParaToInt("pagesize", 10), selectSql,
				getSql("eval.find", getParas())).getList();
		this.renderJson((new JsonUtil(list, "photo")).toString());
	}
//    public void getPage(){
//    	 int shopId = ((SessionModel)this.getSessionAttr("SessionModel")).getShopId();
//         String getPageSql = "select * from mt_theme_eval mte,mt_theme mt where mte.themeId = mt.themeId and mt.shopId=?";
// 		 List<Record> list = Db.find(getPageSql,shopId);
//		int page=(int)Math.ceil((double)list.size()/10);
//		List<Record> pagelist=new ArrayList<Record>();
//		for(int i=1;i<=page;i++){
//			 Map<String,Object> mappage = new HashMap<String,Object>();
//			 mappage.put("i", i);
//			Record record =new Record();
//			record.setColumns(mappage);
//			pagelist.add(record);
//		}
//		this.renderJson(pagelist);
//    }
	// 给web调用分页查询
	public void getEval() {
        int shopId = ((SessionModel)this.getSessionAttr("SessionModel")).getShopId();
        Map<String,Object> map =getParas();
        map.put("shopId", shopId + "");
		String selectSql = "select mtte.id,mtt.themename,mtt.themeId,mtu.headimgurl,mtu.username,mtu.phone,mtte.createdate,mtte.content,mtte.evalstar,mtte.photo,mtte.reply ";
		List<Record> list = Db.paginate(getParaToInt("page",1),getParaToInt("pagesize",10),selectSql,getSql("eval.getEval",map)).getList();
		JsonUtil ju = new JsonUtil();
		ju.put("list", list);
		String getPageSql = "select count(*) count "+ getSql("eval.getEval",map);
		Record count = Db.findFirst(getPageSql);
		int page=(int)Math.ceil(count.getLong("count").doubleValue()/10);
		System.out.println(page);
		List<Record> pagelist=new ArrayList<Record>();
		for(int i=1;i<=page;i++){
			Map<String,Object> mappage =new HashMap<String,Object>();
			mappage.put("i",i);
			Record record =new Record();
			record.setColumns(mappage);
			pagelist.add(record);
		}
		ju.put("page", pagelist);
		this.renderJson(ju.toString());
	}
	public void header(){
		int shopId = ((SessionModel)this.getSessionAttr("SessionModel")).getShopId();
		String themeSql="select themename,themeId from mt_theme where shopId=?";
		List<Record> themelist=Db.find(themeSql,shopId);
		this.renderJson((new JsonUtil(themelist)).toString());
	}
	// 给web调用分页查询
	@SuppressWarnings("unused")
	public void reply() {
		int shopId = ((SessionModel) this.getSessionAttr("SessionModel")).getShopId();
		Db.update("update mt_theme_eval set reply=? where id=?", getPara("reply"), getParaToInt("evalid"));
		this.renderJson("{\"errmsg\":\"\"}");
	}

}